package com.ndp.fb.storage.impl;

import com.ndp.fb.exception.S3Exception;
import com.ndp.fb.storage.S3ResourceManager;
import com.ndp.fb.storage.StorageService;
import com.ndp.fb.util.StringUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

/**
 * Created by infi.he on 2016/4/26.
 */
@Component
public class StorageServiceImpl implements StorageService {
    @Autowired
    S3ResourceManager s3ResourceManager;

    @Override
    public String storeFeedImageAsPublic(String productId, Long materialStyleId, File imageFile) throws IOException {
        return s3ResourceManager.saveForHttp(productId + "/" + materialStyleId + "/image/", imageFile, true);
    }

    @Override
    public String storeFeedImageAsPublic(String productId, Long materialStyleId, File imageFile, String fileName) throws IOException {
        return s3ResourceManager.saveForHttp(productId + "/" + materialStyleId + "/image/", fileName, imageFile, true);
    }

    @Override
    public String storeFeedImageAsPublicExist(String productId, Long materialStyleId, String checkpath) {
        String storePath = productId + "/" + materialStyleId + "/image/";
        String fullFolderName = s3ResourceManager.getFullFolder(storePath);
        String resultPath = fullFolderName + checkpath;
        if (s3ResourceManager.exist(resultPath)) {
            return resultPath;
        } else {
            return null;
        }
    }

    @Override
    public String storeFeedAsPublic(String productId, Long materialStyleId, File feedFile) throws IOException {
        return s3ResourceManager.saveForHttp(productId + "/feed/", materialStyleId + ".xml", feedFile, true);
    }

    @Override
    public String storeMergeFeedAsPublic(String productId, File feedFile) throws IOException {
        return s3ResourceManager.saveForHttp(productId + "/feed/merge/", feedFile.getName(), feedFile, true);
    }

    @Override
    public String storeMaterialStyle(Long materialStyleId, File materialStyleFile) throws IOException {
        return s3ResourceManager.save("material_style/", materialStyleId + "." + FilenameUtils.getExtension(materialStyleFile.getName()), materialStyleFile, true);
    }

    @Override
    public String storeMaterialStyleAsPublic(Long materialStyleId, File materialStyleFile) throws IOException {
        return s3ResourceManager.saveForHttp("material_style/", materialStyleId + "." + FilenameUtils.getExtension(materialStyleFile.getName()), materialStyleFile, true);
    }

    @Override
    public String storeTemplate(Long templateId, File templateFile) throws IOException {
        return s3ResourceManager.save("template/", templateId + "." + FilenameUtils.getExtension(templateFile.getName()), templateFile, true);
    }

    @Override
    public String storeTemplateAsPublic(Long templateId, File templateFile) throws IOException {
        return "http://" + s3ResourceManager.getBucketName() + "/" + s3ResourceManager.saveForHttp("template/", templateId + "." + FilenameUtils.getExtension(templateFile.getName()), templateFile, true);
    }

    @Override
    public String storeResourceAsPublic(Long resourceId, File element, String elementName) throws IOException {
        return s3ResourceManager.saveForHttp("resource/", StringUtil.isEmpty(elementName) ? element.getName() : elementName, element, true);
    }

    @Override
    public String storeElementAsPublic(String code, File element, String elementName) throws IOException {
        return "http://" + s3ResourceManager.getBucketName() + "/" + s3ResourceManager.saveForHttp("element/"+ code +"/", StringUtil.isEmpty(elementName) ? element.getName() : elementName, element, true);
    }

    @Override
    public String storeLogoAsPublic(String code, File logoFile, String name) throws IOException {
        return "http://" + s3ResourceManager.getBucketName() + "/" + s3ResourceManager.saveForHttp("logo/"+ code +"/", StringUtil.isEmpty(name) ? logoFile.getName() : name, logoFile, true);
    }

    @Override
    public String storeFontAsPublic(Long fontId, File font, String fontName) throws IOException {
        return s3ResourceManager.saveForHttp("font/", StringUtil.isEmpty(fontName) ? font.getName() : fontName, font, true);
    }

    @Override
    public String storeFontAsPublicResultHttp(Long fontId, File font, String fontName) throws IOException {
        return "http://" + s3ResourceManager.getBucketName() + "/" + s3ResourceManager.saveForHttp("font/", StringUtil.isEmpty(fontName) ? font.getName() : fontName, font, true);
    }

    @Override
    public File getS3File(String storePath) throws IOException, S3Exception {
        return s3ResourceManager.getFile(storePath);
    }

    @Override
    public String getLocalTempDir() {
        return s3ResourceManager.getS3TempDir();
    }

    @Override
    public String getHttpUrl(String relativeUrl) {
        return "http://" + s3ResourceManager.getBucketName() + "/" + relativeUrl;
    }

	@Override
	public String storePSetExelAsPublic(String productSetId,String productSetName, File excelFile) throws IOException {
		return s3ResourceManager.saveForHttp("product_set_excel/"+productSetId + "/", productSetName, excelFile, true);
	}

    @Override
    public void copyFile(String sourceKey, String descKey) {
        s3ResourceManager.copyFile(sourceKey,descKey);
    }

    public String getFullFolder(String folder) {
        return s3ResourceManager.getFullFolder(folder);
    }
}
